<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="配送模式" prop="model">
        <el-select filterable v-model="queryParams.model" placeholder="请选择配送模式" clearable>
          <el-option
            v-for="dict in dict.type.delivery_model"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否有效" prop="status">
        <el-select filterable v-model="queryParams.status" placeholder="请选择是否有效" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="出发运点" prop="fromServerZoneIdDtLabel">
        <el-input v-model="queryParams.fromServerZoneIdDtLabel" placeholder="请填写出发运点" clearable />
      </el-form-item>
      <el-form-item label="目的网点" prop="toOrg">
        <el-select clearable filterable v-model="queryParams.toOrg" placeholder="请选择目的网点">
          <el-option
            v-for="dict in dict.type.DB_JC_ORG"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="目的运点" prop="toServerZoneIdDtLabel">
        <el-input v-model="queryParams.toServerZoneIdDtLabel" placeholder="请填写目的运点" clearable />
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['extend:customerPrice:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['extend:customerPrice:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['extend:customerPrice:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['system:user:import']"
        >导入
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['extend:customerPrice:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="customerPriceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" fixed/>
      <el-table-column label="编号" align="center" prop="id"/>
      <el-table-column label="客户" align="center" prop="dtMap.customerIdDtLabel"/>
      <el-table-column label="配送模式" align="center" prop="dtMap.modelDtLabel" width="200"/>
      <el-table-column label="出发网点-运点" align="center" width="180">
        <template #default="scope">
          <span>{{ scope.row.fromOrgDtLabel }}-{{ scope.row.fromServerZoneIdDtLabel }}</span>
        </template>
      </el-table-column>
      <el-table-column label="目的网点-运点" align="center" width="180">
        <template #default="scope">
          <span>{{ scope.row.toOrgDtLabel }}-{{ scope.row.toServerZoneIdDtLabel }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收入价格" align="left" prop="incomeNumPrice" width="200">
        <template #default="scope">
          <div>收入价格(按数量):{{ scope.row.incomeNumPrice }}</div>
          <div>收入价格(按重量):{{ scope.row.incomeWeightPrice }}</div>
          <div>冷藏箱20升含以下:{{ scope.row.incomeFreezer0 }}</div>
          <div>冷藏箱21升-60升以下:{{ scope.row.incomeFreezer21 }}</div>
          <div>冷藏箱61升含以上:{{ scope.row.incomeFreezer61 }}</div>
        </template>
      </el-table-column>
      <el-table-column label="干线支出价格" align="left" prop="trunkNumPrice" width="200">
        <template #default="scope">
          <div>干线支出价格(按数量):{{ scope.row.trunkNumPrice }}</div>
          <div>干线支出价格(按重量):{{ scope.row.trunkWeightPrice }}</div>
          <div>冷藏箱20升含以下:{{ scope.row.trunkFreezer0 }}</div>
          <div>冷藏箱21升-60升以下:{{ scope.row.trunkFreezer21 }}</div>
          <div>冷藏箱61升含以上:{{ scope.row.trunkFreezer61 }}</div>
        </template>
      </el-table-column>
      <el-table-column label="配送支出价格" align="left" prop="regionalNumPrice" width="200">
        <template #default="scope">
          <div>配送支出价格(按数量):{{ scope.row.regionalNumPrice }}</div>
          <div>配送支出价格(按重量):{{ scope.row.regionalWeightPrice }}</div>
          <div>冷藏箱20升含以下:{{ scope.row.regionalFreezer0 }}</div>
          <div>冷藏箱21升-60升以下:{{ scope.row.regionalFreezer21 }}</div>
          <div>冷藏箱61升含以上:{{ scope.row.regionalFreezer61 }}</div>
        </template>
      </el-table-column>
      <el-table-column label="取货费" align="center" prop="pickupPrice"/>
      <el-table-column label="其他支出价格" align="center" prop="otherSpendPrice"/>
      <el-table-column label="是否有效" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['extend:customerPrice:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['extend:customerPrice:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户单价对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="160px" label-position="left">
        <el-divider content-position="left">基础信息</el-divider>
        <el-row :gutter="10">
          <el-col :span="24">
            <el-form-item label="配送模式" prop="models">
              <el-select filterable v-model="form.models" placeholder="请选择配送模式" multiple style="width: 90%;">
                <el-option
                  v-for="dict in dict.type.delivery_model"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否有效" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="出发网点" prop="fromOrg">
              <el-select clearable filterable v-model="form.fromOrg" placeholder="请选择出发网点" @change="getFromServerZone">
                <el-option
                  v-for="dict in dict.type.DB_JC_ORG"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="目的网点" prop="toOrg">
              <el-select clearable filterable v-model="form.toOrg" placeholder="请选择目的网点" @change="getToServerZone">
                <el-option
                  v-for="dict in dict.type.DB_JC_ORG"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出发运点" prop="fromServerZoneId">
              <el-select filterable v-model="form.fromServerZoneId" placeholder="请选择出发运点">
                <el-option
                  v-for="dict in fromServerZones"
                  :key="dict.id"
                  :label="dict.jcZoneIdDtLabel+'(距离:' + dict.mileage + ')'"
                  :value="dict.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="目的运点" prop="toServerZoneId">
              <el-select filterable v-model="form.toServerZoneId" placeholder="请选择目的运点">
                <el-option
                  v-for="dict in toServerZones"
                  :key="dict.id"
                  :label="dict.jcZoneIdDtLabel+'(距离:' + dict.mileage + ')'"
                  :value="dict.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="left">收入价格</el-divider>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="收入价格(按数量)" prop="incomeNumPrice">
              <el-input-number size="mini" :step="1" :min="0" v-model="form.incomeNumPrice"
                               placeholder="请输入收入价格(按数量)"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收入价格(按重量)" prop="incomeWeightPrice">
              <el-input-number size="mini" :step="1" :min="0" v-model="form.incomeWeightPrice"
                               placeholder="请输入收入价格(按重量)"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="冷藏箱20升含以下" prop="incomeFreezer0">
              <el-input-number size="mini" :step="1" :min="0" v-model="form.incomeFreezer0"
                               placeholder="请输入冷藏箱20升含以下"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="冷藏箱21升-60升以下" prop="incomeFreezer21">
              <el-input-number size="mini" :step="1" :min="0" v-model="form.incomeFreezer21"
                               placeholder="请输入冷藏箱21升-60升以下"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="冷藏箱61升含以上" prop="incomeFreezer61">
              <el-input-number size="mini" :step="1" :min="0" v-model="form.incomeFreezer61"
                               placeholder="请输入冷藏箱61升含以上"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider content-position="center">分段体积价格信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddJcCustomerVolumePrice">添加
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteJcCustomerVolumePrice">删除
            </el-button>
          </el-col>
        </el-row>
        <el-table :data="jcCustomerVolumePriceList" :row-class-name="rowJcCustomerVolumePriceIndex"
                  @selection-change="handleJcCustomerVolumePriceSelectionChange" ref="jcCustomerVolumePrice">
          <el-table-column type="selection" width="50" align="center"/>
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="开始体积(包括)" prop="startValue">
            <template #default="scope">
              <el-input-number size="mini" :step="1" :min="0" v-model="scope.row.startValue"
                               placeholder="请输入开始体积(包括)"/>
            </template>
          </el-table-column>
          <el-table-column label="结束体积(不包括)" prop="endValue">
            <template #default="scope">
              <el-input-number size="mini" :step="1" :min="1" v-model="scope.row.endValue"
                               placeholder="请输入结束体积(不包括)"/>
            </template>
          </el-table-column>
          <el-table-column label="体积价格" prop="priceValue">
            <template #default="scope">
              <el-input-number size="mini" :step="1" :min="1" v-model="scope.row.priceValue"
                               placeholder="请输入体积价格"/>
            </template>
          </el-table-column>

          <el-table-column label="干线支出价格" prop="trunkPrice">
            <template #default="scope">
              <el-input-number size="mini" :step="1" :min="0" v-model="scope.row.trunkPrice"
                               placeholder="请输入干线支出价格"/>
            </template>
          </el-table-column>

          <el-table-column label="配送支出价格" prop="regionalPrice">
            <template #default="scope">
              <el-input-number size="mini" :step="1" :min="0" v-model="scope.row.regionalPrice"
                               placeholder="请输入配送支出价格"/>
            </template>
          </el-table-column>

        </el-table>

        <el-divider content-position="left">干线支出价格(按数量)</el-divider>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="干线支出价格(按数量)" prop="trunkNumPrice">
              <el-input-number size="mini" :step="1" :min="0" v-model="form.trunkNumPrice"
                               placeholder="请输入干线支出价格(按数量)"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="干线支出价格(按重量)" prop="trunkWeightPrice">
              <el-input-number size="mini" :step="1" :min="0" v-model="form.trunkWeightPrice"
                               placeholder="请输入干线支出价格(按重量)"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="冷藏箱20升含以下" prop="trunkFreezer0">
              <el-input-number size="mini" :step="1" :min="0" v-model="form.trunkFreezer0"
                               placeholder="请输入冷藏箱20升含以下"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="冷藏箱21升-60升以下" prop="trunkFreezer21">
              <el-input-number size="mini" :step="1" :min="0" v-model="form.trunkFreezer21"
                               placeholder="请输入冷藏箱21升-60升以下"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="冷藏箱61升含以上" prop="trunkFreezer61">
              <el-input-number size="mini" :step="1" :min="0" v-model="form.trunkFreezer61"
                               placeholder="请输入冷藏箱61升含以上"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="left">配送(到终端)支出价格</el-divider>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="配送支出价格(按数量)" prop="regionalNumPrice">
              <el-input-number size="mini" :step="1" :min="0" v-model="form.regionalNumPrice"
                               placeholder="请输入配送支出价格(按数量)"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="配送支出价格(按重量)" prop="regionalWeightPrice">
              <el-input-number size="mini" :step="1" :min="0" v-model="form.regionalWeightPrice"
                               placeholder="请输入配送支出价格(按重量)"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="冷藏箱20升含以下" prop="regionalFreezer0">
              <el-input-number size="mini" :step="1" :min="0" v-model="form.regionalFreezer0"
                               placeholder="请输入冷藏箱20升含以下"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="冷藏箱21升-60升以下" prop="regionalFreezer21">
              <el-input-number size="mini" :step="1" :min="0" v-model="form.regionalFreezer21"
                               placeholder="请输入冷藏箱21升-60升以下"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="冷藏箱61升含以上" prop="regionalFreezer61">
              <el-input-number size="mini" :step="1" :min="0" v-model="form.regionalFreezer61"
                               placeholder="请输入冷藏箱61升含以上"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="left">其他价格(同一个客户的取货费用只需要填一次)</el-divider>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="其他支出价格((按件)" prop="otherSpendPrice">
              <el-input-number size="mini" :step="1" :min="0" v-model="form.otherSpendPrice"
                               placeholder="请输入其他支出价格"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="取货费用(按件)" prop="pickupPrice">
              <el-input-number size="mini" :step="1" :min="0" v-model="form.pickupPrice" placeholder="请输入取货费用"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 导入对话框 -->
    <el-dialog :close-on-click-modal="false" :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport"/>
            是否更新已经存在的价格数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addCustomerPrice,
  delCustomerPrice,
  getCustomerPrice,
  listCustomerPrice,
  updateCustomerPrice
} from '@/api/extend/customerPrice'
import {getCustomer} from '@/api/extend/customer'
import {getZoneByOrgId} from '@/api/extend/serverZone'
import {getToken} from '@/utils/auth'

export default {
  name: 'CustomerPrice',
  dicts: ['delivery_model', 'JSFS', 'sys_yes_no', 'DB_JC_ORG'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedJcCustomerVolumePrice: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户单价表格数据
      customerPriceList: [],
      // 分段体积价格表格数据
      jcCustomerVolumePriceList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerId: null,
        jcOrganizationId: null,
        model: null,
        fromOrg: null,
        fromServerZoneIdDtLabel: null,
        toOrg: null,
        toServerZoneIdDtLabel: null,
        fromServerZoneId: null,
        toServerZoneId: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        customerId: [
          {required: true, message: "客户ID不能为空", trigger: "blur"}
        ],
        jcOrganizationId: [
          {required: true, message: "组织机构不能为空", trigger: "blur"}
        ],
        model: [
          {required: true, message: "配送模式不能为空", trigger: "blur"}
        ],
        fromOrg: [
          {required: true, message: "出发网点不能为空", trigger: "blur"}
        ],
        toOrg: [
          {required: true, message: "目的网点不能为空", trigger: "blur"}
        ],
        fromServerZoneId: [
          {required: true, message: "出发运点不能为空", trigger: "blur"}
        ],
        toServerZoneId: [
          {required: true, message: "目的运点不能为空", trigger: "blur"}
        ],
        incomeNumPrice: [
          {required: true, message: "收入价格(按数量)不能为空", trigger: "blur"}
        ],
        incomeFreezer0: [
          {required: true, message: "冷藏箱20升含以下不能为空", trigger: "blur"}
        ],
        incomeFreezer21: [
          {required: true, message: "冷藏箱21升-60升以下不能为空", trigger: "blur"}
        ],
        incomeFreezer61: [
          {required: true, message: "冷藏箱61升含以上不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: '是否有效不能为空', trigger: 'change'}
        ]
      },
      customerId: null,
      customer: null,
      fromServerZones: [],
      toServerZones: [],
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: '',
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: {Authorization: 'Bearer ' + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + '/extend/customerPrice/importData'
      },
    }
  },
  created() {
    const customerId = this.$route.params && this.$route.params.customerId;
    if (customerId) {
      this.customerId = customerId
      this.getCustomerInfo(customerId)
    }
    this.getList()
  },
  methods: {
    getCustomerInfo(customerId) {
      getCustomer(customerId).then(response => {
        this.customer = response.data
      })
    },
    /** 查询客户单价列表 */
    getList() {
      this.loading = true
      this.queryParams.customerId = this.customerId
      listCustomerPrice(this.queryParams).then(response => {
        this.customerPriceList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customerId: this.customer.id,
        jcOrganizationId: this.customer.jcOrganizationId,
        model: null,
        fromOrg: null,
        toOrg: null,
        fromServerZoneId: null,
        toServerZoneId: null,
        incomeNumPrice: null,
        incomeWeightPrice: null,
        incomeFreezer0: null,
        incomeFreezer21: null,
        incomeFreezer61: null,
        trunkNumPrice: null,
        trunkWeightPrice: null,
        trunkFreezer0: null,
        trunkFreezer21: null,
        trunkFreezer61: null,
        regionalNumPrice: null,
        regionalWeightPrice: null,
        regionalFreezer0: null,
        regionalFreezer21: null,
        regionalFreezer61: null,
        pickupPrice: null,
        otherSpendPrice: null,
        status: 'Y',
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.jcCustomerVolumePriceList = [];
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加客户单价'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getCustomerPrice(id).then(response => {
        const data = response.data
        this.getFromServerZone(data.fromOrg)
        this.getToServerZone(data.toOrg)
        this.form = data
        if (data.jcCustomerVolumePriceList) {
          this.jcCustomerVolumePriceList = data.jcCustomerVolumePriceList;
        }
        this.open = true
        this.title = '修改客户单价'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.jcCustomerVolumePriceList = this.jcCustomerVolumePriceList;
          if (this.form.id != null) {
            updateCustomerPrice(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addCustomerPrice(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除客户单价编号为"' + ids + '"的数据项？').then(function () {
        return delCustomerPrice(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 分段体积价格序号 */
    rowJcCustomerVolumePriceIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    getFromServerZone(item) {
      this.fromServerZones = []
      this.form.fromServerZoneId = null
      getZoneByOrgId(item).then(res => {
        this.fromServerZones = res.data
      })
    },
    getToServerZone(item) {
      this.toServerZones = []
      this.form.toServerZoneId = null
      getZoneByOrgId(item).then(res => {
        this.toServerZones = res.data
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/extend/customerPrice/export', {
        ...this.queryParams
      }, `customerPrice_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = '客户价格导入'
      this.upload.open = true
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('extend/customerPrice/importTemplate', {}, `price_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      this.$alert('<div style=\'overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;\'>' + response.msg + '</div>', '导入结果', {dangerouslyUseHTMLString: true})
      this.getList()
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit()
    },
    /** 分段体积价格添加按钮操作 */
    handleAddJcCustomerVolumePrice() {
      let obj = {};
      obj.startValue = "";
      obj.endValue = "";
      obj.priceValue = 0;
      obj.trunkNumPrice = 0;
      obj.regionalNumPrice = 0;
      this.jcCustomerVolumePriceList.push(obj);
    },
    /** 分段体积价格删除按钮操作 */
    handleDeleteJcCustomerVolumePrice() {
      if (this.checkedJcCustomerVolumePrice.length === 0) {
        this.$modal.msgError("请先选择要删除的分段体积价格数据");
      } else {
        const jcCustomerVolumePriceList = this.jcCustomerVolumePriceList;
        const checkedJcCustomerVolumePrice = this.checkedJcCustomerVolumePrice;
        this.jcCustomerVolumePriceList = jcCustomerVolumePriceList.filter(function (item) {
          return checkedJcCustomerVolumePrice.indexOf(item.index) === -1
        });
      }
    },
    /** 复选框选中数据 */
    handleJcCustomerVolumePriceSelectionChange(selection) {
      this.checkedJcCustomerVolumePrice = selection.map(item => item.index)
    },
  }
}
</script>
